package com.etc.service;

import com.etc.dao.ICategoryDao;
import com.etc.entity.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryService {

   @Autowired
   ICategoryDao categoryDao;
   
   public void insert(Category c) {
	   categoryDao.insert(c);
   }
   
   public List<Category> queryAll (int page) {
	   List<Category> categoryList = categoryDao.queryAll(page);
	   List<Category> categoriesAll = categoryDao.queryAll2();
	   
	   for (Category c : categoryList) {
		   if (c.getParentId() == 0) {
			   c.setParent("无父类");
		   } else {
			   String title = categoriesAll.stream().filter(item->item.getCid() == c.getParentId()).findFirst().get().getCtitle();
			   c.setParent(title);
			   System.out.println(title);
		   }
	   }
	   return categoryList;
   }
   
   public List<Category> queryAll2 () {
	   return categoryDao.queryAll2();
   }
}
